<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\PerformanceService;
use App\Services\CacheService;
use App\Services\LogService;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PerformanceController extends Controller
{
    use ApiResponse;

    protected $performanceService;
    protected $cacheService;
    protected $logService;

    public function __construct(
        PerformanceService $performanceService,
        CacheService $cacheService,
        LogService $logService
    ) {
        $this->performanceService = $performanceService;
        $this->cacheService = $cacheService;
        $this->logService = $logService;
    }

    /**
     * 获取系统性能指标
     */
    public function systemMetrics(): JsonResponse
    {
        try {
            $metrics = $this->performanceService->getSystemMetrics();
            
            return $this->success([
                'metrics' => $metrics
            ], '获取系统性能指标成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取数据库性能指标
     */
    public function databaseMetrics(): JsonResponse
    {
        try {
            $metrics = $this->performanceService->getDatabaseMetrics();
            
            return $this->success([
                'metrics' => $metrics
            ], '获取数据库性能指标成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取缓存性能指标
     */
    public function cacheMetrics(): JsonResponse
    {
        try {
            $metrics = $this->performanceService->getCacheMetrics();
            
            return $this->success([
                'metrics' => $metrics
            ], '获取缓存性能指标成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取缓存信息
     */
    public function cacheInfo(): JsonResponse
    {
        try {
            $info = $this->cacheService->getInfo();
            
            return $this->success([
                'info' => $info
            ], '获取缓存信息成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 清空缓存
     */
    public function clearCache(): JsonResponse
    {
        try {
            $this->cacheService->flush();
            
            return $this->success([], '缓存清空成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取日志统计
     */
    public function logStats(): JsonResponse
    {
        try {
            $stats = $this->logService->getLogStats();
            
            return $this->success([
                'stats' => $stats
            ], '获取日志统计成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 性能基准测试
     */
    public function benchmark(Request $request): JsonResponse
    {
        $request->validate([
            'iterations' => 'sometimes|integer|min:1|max:1000',
        ]);

        try {
            $iterations = $request->input('iterations', 100);
            
            // 简单的基准测试
            $result = $this->performanceService->benchmark(function () {
                // 模拟一些操作
                $data = [];
                for ($i = 0; $i < 1000; $i++) {
                    $data[] = md5($i);
                }
                return $data;
            }, $iterations);
            
            return $this->success([
                'benchmark' => $result
            ], '性能基准测试完成');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 检查性能阈值
     */
    public function checkThresholds(): JsonResponse
    {
        try {
            $result = $this->performanceService->checkPerformanceThresholds();
            
            return $this->success([
                'thresholds' => $result
            ], '性能阈值检查完成');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }

    /**
     * 获取活动日志
     */
    public function activityLogs(Request $request): JsonResponse
    {
        $request->validate([
            'limit' => 'sometimes|integer|min:1|max:100',
        ]);

        try {
            $limit = $request->input('limit', 50);
            $logs = $this->logService->getActivityLogs(null, $limit);
            
            return $this->success([
                'logs' => $logs
            ], '获取活动日志成功');

        } catch (\Exception $e) {
            return $this->handleException($e);
        }
    }
}